package com.lekang.system.service.impl;

import java.util.List;
import com.lekang.common.utils.DateUtils;
import com.lekang.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.system.mapper.NursingLevelMapper;
import com.lekang.system.domain.NursingLevel;
import com.lekang.system.service.INursingLevelService;
import com.lekang.system.mapper.NursingPlanLevelBindingMapper;
import com.lekang.system.domain.NursingPlanLevelBinding;

/**
 * 护理等级Service业务层处理
 * 
 * @author lekang
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService 
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingPlanLevelBindingMapper planLevelBindingMapper;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        // 设置创建人为当前登录用户ID
        try {
            nursingLevel.setCreator(SecurityUtils.getUserId());
        } catch (Exception ignored) {}
        int result = nursingLevelMapper.insertNursingLevel(nursingLevel);
        // 新增成功后，若前端传入了计划ID（复用extraField1传递），则写入绑定关系
        if (result > 0 && nursingLevel.getExtraField1() != null && !"".equals(nursingLevel.getExtraField1())) {
            try {
                Long planId = Long.valueOf(nursingLevel.getExtraField1());
                NursingPlanLevelBinding binding = new NursingPlanLevelBinding();
                binding.setPlanId(planId);
                binding.setLevelId(nursingLevel.getId());
                planLevelBindingMapper.insertBinding(binding);
            } catch (NumberFormatException ignored) { }
        }
        return result;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        int result = nursingLevelMapper.updateNursingLevel(nursingLevel);
        // 更新绑定：若传入了新的计划ID（extraField1），则重建绑定
        if (result > 0 && nursingLevel.getId() != null && nursingLevel.getExtraField1() != null && !"".equals(nursingLevel.getExtraField1())) {
            planLevelBindingMapper.deleteBindingByLevelId(nursingLevel.getId());
            try {
                Long planId = Long.valueOf(nursingLevel.getExtraField1());
                NursingPlanLevelBinding binding = new NursingPlanLevelBinding();
                binding.setPlanId(planId);
                binding.setLevelId(nursingLevel.getId());
                planLevelBindingMapper.insertBinding(binding);
            } catch (NumberFormatException ignored) { }
        }
        return result;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        return nursingLevelMapper.deleteNursingLevelByIds(ids);
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        return nursingLevelMapper.deleteNursingLevelById(id);
    }
}
